package com.example.animations.controls;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

public class PathView extends View {
    private RectF _rect;
    private Paint _paint;

    private float _rotation;
    private float _cornerRadius;
    private float _stroke;

    private int _centerX;
    private int _centerY;
    private float _viewRadius;

    public PathView(Context context) {
        super(context);
        initialize();
    }

    public PathView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initialize();
    }

    public PathView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initialize();
    }

    private void initialize() {
        _paint = new Paint();
        _paint.setColor(Color.RED);
        _paint.setStyle(Paint.Style.STROKE);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        if (_rect == null) {
            _rect = new RectF();
        }

        if (_centerX == 0 || _centerY == 0) {
            _centerX = getWidth() / 2;
            _centerY = getHeight() / 2;
        }

        _paint.setStrokeWidth(_stroke);

        _rect.top = _centerY - _viewRadius;
        _rect.right = _centerX + _viewRadius;
        _rect.bottom = _centerY + _viewRadius;
        _rect.left = _centerX - _viewRadius;

        canvas.rotate(_rotation, _centerX, _centerY);
        canvas.drawRoundRect(_rect, _cornerRadius, _cornerRadius, _paint);

        invalidate();
    }

    public void setRotation(float degrees) {
        _rotation = degrees;
    }

    public void setSize(float size) {
        _viewRadius = size / 2;
    }

    public void setStrokeWidth(float width) {
        _stroke = width;
    }

    public void setCornerRadius(float radius) {
        _cornerRadius = radius;
    }

    public void initValues(float rotation, float size, float strokeWidth) {
        setRotation(rotation);
        setSize(size);
        setStrokeWidth(strokeWidth);
    }

    public void updateAnimatedValues(float rotation, float cornerRadius) {
        _rotation = rotation;
        _cornerRadius = cornerRadius;
    }
}
